<link href="/classico/public/assets/all-site/css/jquery-ui.min.css"
      rel="stylesheet" type="text/css"/>
<link href="/classico/public/assets/back-end/css/user/add.css"
      rel="stylesheet" type="text/css"/>
<script type="text/javascript"
        src="/classico/public/assets/all-site/js/jquery-ui.min.js"></script>
<div class="system-messages">
    <ul>
        <?php
        $messages = $this->messages;
        foreach ($messages as $message) :
            ?>
            <li><?= $message ?></li>
        <?php
        endforeach
        ?>
    </ul>
</div>
<?php
/** @var UserPOJO $userPOJO */
$userPOJO = $this->userPOJO;
?>
<div class="admin-input-form-wrap">
    <form method="post">
        <div id="basic-info" class="admin-input-form">
            <table>
                <tr>
                    <td><label for="username">Username</label></td>
                    <td>
                        <input id="username" type="text" name="username"
                               value="<?= $userPOJO == null ?
                                   $this->username : $userPOJO->Username ?>"
                            <?= $this->layout == 'update' ? 'disabled="disabled"' : '' ?>/>
                        <?php
                        if ($this->layout == 'update') :
                            ?>
                            <input type="hidden" name="username"
                                   value="<?= $userPOJO == null ?
                                       $this->username : $userPOJO->Username ?>"/>
                        <?php
                        endif
                        ?>
                    </td>
                </tr>
                <tr>
                    <td><label for="email">Email</label></td>
                    <td>
                        <input id="email" type="text" name="email"
                               value="<?= $userPOJO == null ?
                                   $this->email : $userPOJO->Email ?>"
                            <?= $this->layout == 'update' ? 'disabled="disabled"' : '' ?>/>
                        <?php
                        if ($this->layout == 'update') :
                            ?>
                            <input type="hidden" name="email"
                                   value="<?= $userPOJO == null ?
                                       $this->email : $userPOJO->Email ?>"/>
                        <?php
                        endif
                        ?>
                    </td>
                </tr>
                <tr>
                    <td><label for="password">Password</label></td>
                    <td>
                        <input id="password" type="password" name="password"
                               value="<?= $userPOJO == null ?
                                   $this->password : $userPOJO->Password ?>"/>
                    </td>
                </tr>
                <tr>
                    <td><label for="confirm">Confirm</label></td>
                    <td>
                        <input id="confirm" type="password" name="confirm"
                               value="<?= $userPOJO == null ?
                                   $this->password : $userPOJO->Password ?>"/>
                    </td>
                </tr>
                <tr>
                    <td><label for="real-name">Real Name</label></td>
                    <td>
                        <input id="real-name" type="text" name="realName"
                               value="<?= $userPOJO == null ?
                                   $this->realName : $userPOJO->RealName ?>"/>
                    </td>
                </tr>
                <tr>
                    <td><label for="birthday">Birthday</label></td>
                    <td>
                        <p>
                            <input id="birthday" type="text" name="birthday"
                                   value="<?= $userPOJO == null ?
                                       $this->birthday : $userPOJO->Birthday->get('d - M - y') ?>"/>
                        </p>
                    </td>
                </tr>
                <tr>
                    <td><label for="mobile-phone">Mobile Phone</label></td>
                    <td>
                        <input id="mobile-phone" type="text" name="mobilePhone"
                               value="<?= $userPOJO == null ?
                                   $this->mobilePhone : $userPOJO->MobilePhone ?>"/>
                    </td>
                </tr>
                <tr>
                    <td><label for="user-type-id">User Type</label></td>
                    <td>
                        <select id="user-type-id" name="userTypeID">
                            <option value="0">-- User Type --</option>
                            <?php
                            $selectedUserTypeID = $userPOJO == null ?
                                $this->selectedUserTypeID : $userPOJO->UserTypeID;

                            /** @var UserTypePOJO[] $userTypePOJOs */
                            $userTypePOJOs = $this->userTypePOJOs;
                            foreach ($userTypePOJOs as $userTypePOJO) :
                                if ($selectedUserTypeID && $userTypePOJO->ID == $selectedUserTypeID) :
                                    ?>
                                    <option value="<?= $userTypePOJO->ID ?>"
                                            selected="selected">
                                        <?= $userTypePOJO->Name ?>
                                    </option>
                                <?php
                                endif;
                                if (!$selectedUserTypeID || $userTypePOJO->ID != $selectedUserTypeID) :
                                    ?>
                                    <option value="<?= $userTypePOJO->ID ?>">
                                        <?= $userTypePOJO->Name ?>
                                    </option>
                                <?php
                                endif;
                            endforeach
                            ?>
                        </select>
                    </td>
                </tr>
            </table>
            <input type="hidden" name="isPost" value=""/>
        </div>
    </form>
</div>
<script type="text/javascript">
    $(function () {
        $('#birthday').datepicker({
            dateFormat: 'dd - mm - yy'
        });
    });
</script>
<script type="">
    function submitForm(task) {
        var username = $('#username').val().trim();
        var email = $('#email').val().trim();
        var password = $('#password').val();
        var realName = $('#real-name').val().trim();
        var birthday = $('#birthday').val();
        var mobilePhone = $('#mobile-phone').val().trim();
        var userTypeID = $('#user-type-id').val();
        if (!username || !email || !password || !realName
            || !birthday || !mobilePhone || userTypeID == 0) {
            var content = '';
            if (!username) {
                content += 'Username, ';
            }
            if (!email) {
                content += 'Email, ';
            }
            if (!password) {
                content += 'Password, ';
            }
            if (!confirm) {
                content += 'Confirm, ';
            }
            if (!realName) {
                content += 'RealName, ';
            }
            if (!birthday) {
                content += 'Birthday, ';
            }
            if (!mobilePhone) {
                content += 'MobilePhone, ';
            }
            if (userTypeID == 0) {
                content += 'UserType, ';
            }
            content = content.substring(0, content.length - 2);
            content += ' cannot be blank!';

            var messageBlock = $('.system-messages > ul');
            messageBlock.html('');
            messageBlock.append('<li>' + content + '</li>');

            var confirm = $('#confirm').val();
            if (password != confirm) {
                messageBlock.append('<li>Password and confirm do not match!!!</li>');
            }
        } else {
            var form = $('.admin-input-form-wrap > form');
            form.append('<input type="hidden" name="task" value="' + task + '"/>');
            form.attr('action', location.href);
            form.submit();
        }
    }

    $('#tool-btn-apply').click(function () {
        var task = 'apply';
        submitForm(task);
    });

    $('#tool-btn-applynclose').click(function () {
        var task = 'apply-and-close';
        submitForm(task);
    });

    $('#tool-btn-applynnew').click(function () {
        var task = 'apply-and-new';
        submitForm(task);
    });
</script>